////////////////////////////////////////////////////////////////////
//                                                                //
//   Header File : ini.h                                          //
//   Author : Joe Zheng(Jaredz@163.com)                           //
//   Create : 2010-01-18                                          //
//   Update :                                                     //
//                                                                //
////////////////////////////////////////////////////////////////////

#ifndef __INI_H__
#define __INI_H__

#pragma once
#define LTM_LEN 256
#define LTM_MIN 32
//////////////////////////////////////////////////////////////////////////
// CIni
class CIni
{
public:
	CIni(LPCTSTR pszFile = NULL);
	~CIni(void);

	BOOL CreateINI(LPCTSTR pszFile);
	void SetINI(LPCTSTR pszFile);

	BOOL IsSection(LPCTSTR pszSection);
	BOOL CreateSetion(LPCTSTR pszSection);

	BOOL GetKeyValue(LPCTSTR pszSection, LPCTSTR pszKey, LPTSTR pszValue, INT nLength);
	BOOL SetKeyValue(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszValue);
	UINT GetKeyIntValue(LPCTSTR pszSection, LPCTSTR pszKey, INT nDefault = 0);	
	BOOL SetKeyIntValue(LPCTSTR pszSection, LPCTSTR pszKey, INT nValue);	
	BOOL GetKeyStructValue(LPCTSTR pszSection, LPCTSTR pszKey, void* pStruct, UINT uSizeStruct);
	BOOL SetKeyStructValue(LPCTSTR pszSection, LPCTSTR pszKey, void* pStruct, UINT uSizeStruct);
private:
	CIni(const CIni&);
	CIni& operator=(const CIni&);
public:
	TCHAR  m_szINI[MAX_PATH]; 
};

inline CIni::CIni(LPCTSTR pszFile)
{ 
	SetINI(pszFile);
}

inline CIni::~CIni(void)
{
}

inline CIni::CIni(const CIni&)
{
}

inline CIni& CIni::operator=(const CIni&)
{
	return (*this);
}

inline BOOL CIni::CreateINI(LPCTSTR pszFile)
{
	HANDLE h = ::CreateFile(pszFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (h != INVALID_HANDLE_VALUE)
	{  
		::CloseHandle(h);
		SetINI(pszFile);
	}
	return (h != INVALID_HANDLE_VALUE);
}

inline void CIni::SetINI(LPCTSTR pszFile)
{
	if (pszFile == NULL)
	{
		memset(m_szINI, 0, sizeof(TCHAR) * LTM_LEN);
	}
	else
	{
		_tcsncpy(m_szINI, pszFile, (LTM_LEN - 1));
		m_szINI[LTM_LEN - 1] = 0;
	}
}

inline BOOL CIni::IsSection(LPCTSTR pszSection)
{
	TCHAR szTmp[LTM_MIN] = { 0 };
	return (::GetPrivateProfileString(pszSection, NULL, _T(""), szTmp, LTM_MIN, m_szINI) > 0);
}

inline BOOL CIni::CreateSetion(LPCTSTR pszSection)
{
	return ::WritePrivateProfileSection(pszSection, _T(""), m_szINI);
}

inline BOOL CIni::GetKeyValue(LPCTSTR pszSection, LPCTSTR pszKey, LPTSTR pszValue, INT nLength)
{
	assert(nLength > 0);
	return (::GetPrivateProfileString(pszSection, pszKey, _T(""), pszValue, nLength, m_szINI) > 0);
}

inline BOOL CIni::SetKeyValue(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszValue)
{
	return ::WritePrivateProfileString(pszSection, pszKey, pszValue, m_szINI);
}

inline UINT CIni::GetKeyIntValue(LPCTSTR pszSection, LPCTSTR pszKey, INT nDefault)
{
	return ::GetPrivateProfileInt(pszSection, pszKey, nDefault, m_szINI);
}

inline BOOL CIni::SetKeyIntValue(LPCTSTR pszSection, LPCTSTR pszKey, INT nValue)
{
	TCHAR szTmp[LTM_MIN] = { 0 };
	_stprintf(szTmp, _T("%d"), nValue);
	return ::WritePrivateProfileString(pszSection, pszKey, szTmp, m_szINI);
}

inline BOOL CIni::GetKeyStructValue(LPCTSTR pszSection, LPCTSTR pszKey, void* pStruct, UINT uSizeStruct)
{
	return ::GetPrivateProfileStruct(pszSection, pszKey, pStruct, uSizeStruct, m_szINI);
}

inline BOOL CIni::SetKeyStructValue(LPCTSTR pszSection, LPCTSTR pszKey, void* pStruct, UINT uSizeStruct)
{
	return ::WritePrivateProfileStruct(pszSection, pszKey, pStruct, uSizeStruct, m_szINI);
}

#endif // __INI_H__